<?php
$pageTitle = "费用填报";
require_once 'header.php';

// 获取ID
if (!isset($_GET['id'])) {
    header('Location: index.php');
    exit;
}
$training_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$school = $_SESSION['school'];

$success = '';

// 查询培训信息
$stmt = $pdo->prepare("SELECT * FROM trainings WHERE id = ?");
$stmt->execute([$training_id]);
$training = $stmt->fetch();

if (!$training) {
    header('Location: index.php');
    exit;
}

// 检查是否已存在费用记录
$stmt = $pdo->prepare("SELECT * FROM trainings_cost WHERE trainings_id = ? AND school = ?");
$stmt->execute([$training_id, $school]);
$cost_record = $stmt->fetch();
$cost_data = $cost_record ?: [
    'cost' => 0,
    'other_cost' => 0,
];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 处理表单提交
    $cost = floatval($_POST['cost']);
    $other_cost = floatval($_POST['other_cost']);
    
    if ($cost_record) {
        // 更新现有记录
        $stmt = $pdo->prepare("UPDATE trainings_cost SET cost = ?, other_cost = ? WHERE id = ?");
        $stmt->execute([$cost, $other_cost, $cost_record['id']]);
    } else {
        // 创建新记录
        $stmt = $pdo->prepare("INSERT INTO trainings_cost (trainings_id, school, cost, other_cost) VALUES (?, ?, ?, ?)");
        $stmt->execute([$training_id, $school, $cost, $other_cost]);
    }
    $cost_data['cost'] = $cost;
    $cost_data['other_cost'] = $other_cost;

    $success = '填报完成';
}
?>

<div class="container mt-5">
    <h2 class="mb-4">填报培训费用</h2>

    <div class="table-responsive">
        <table class="table table-striped table-hover table-bordered">
            <thead>
                <tr>
                    <th>主办科室或学校</th>
                    <th>主题</th>
                    <th>时间</th>
                    <th>培训地点</th>
                    <th>任务或自主</th>
                </tr>
            </thead>
            <tbody>
                    <tr>
                        <td><?= htmlspecialchars($training['department']) ?></td>
                        <td><?= htmlspecialchars($training['title']) ?></td>
                        <td><?= htmlspecialchars($training['start_time'] . ' 至 ' . $training['end_time']) ?></td>
                        <td><?= htmlspecialchars($training['location']) ?></td>
                        <td><?= htmlspecialchars($training['type']) ?></td>
                    </tr>
            </tbody>
        </table>
    </div>

    <?php if ($success): ?>
        <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
    <?php endif; ?>
    
    <form method="post">
        <div class="mb-3">
            <label for="school" class="form-label">学校</label>
            <input type="text" class="form-control" id="school" value="<?= htmlspecialchars($school) ?>" readonly>
        </div>
        <div class="mb-3">
            <label for="cost" class="form-label">费用</label>
            <input type="number" step="0.01" class="form-control" id="cost" name="cost" value="<?= $cost_data['cost'] ?>" required>
        </div>
        <div class="mb-3">
            <label for="other_cost" class="form-label">其他费用</label>
            <input type="number" step="0.01" class="form-control" id="other_cost" name="other_cost" value="<?= $cost_data['other_cost'] ?>" required>
        </div>
        <button type="submit" class="btn btn-primary">提交</button>
    </form>
</div>

<?php require_once 'footer.php'; ?>